package dev.projectnightfall.main;

import java.io.IOException;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handles all console output and file logging.
 */
public class ConsoleLog {
    //must create thread which operates off array list and clears it... therefore not choking main thread
    //oh, also need to sort that log thead, mind got derailed

    private static FileHandler logFileHandler;
    private static Logger log;
    private static boolean debug;

    /**
     * Initialises the ConsoleLog and prepares it for external calls.
     */
    public void initialise() {
        try {
            logFileHandler = new FileHandler("game.log");
            log = Logger.getLogger("ProjectNightfall");
            log.addHandler(logFileHandler);
            debug = true;
        } catch (IOException ex) {
            System.err.println("Failed to initialise ConsoleLog.");
            ex.printStackTrace();
        }
    }

    /**
     * Prints out data to the console and then logs it to the log file.
     *
     * @param message Message to print out to console and to log to file.
     * @param l Level of the message to print and log to file.
     */
    public static void print(String message, Level l) {
        String output = "";
        if (l == Level.SEVERE) {
            output = "[SEVERE][" + getTimeStamp() + "]";
        } else if (l == Level.WARNING) {
            output = "[WARNING][" + getTimeStamp() + "]";
        } else if (l == Level.INFO) {
            output = "[INFO][" + getTimeStamp() + "]";
        } else {
            output = "[INFO][" + getTimeStamp() + "]";
        }
        output = output + message;
        System.out.println(output);
        ConsoleLog.log(l, output);

    }

    /**
     * Checks if Debug mode is enabled.
     *
     * @return Returns a boolean
     */
    public static boolean isDebug() {
        return debug;
    }

    private static void log(Level l, String message) {
        log.log(l, message);
    }

    /**
     * Returns the current Unix timestamp.
     *
     * @return Returns the current Unix timestamp
     */
    public static String getTimeStamp() {
        long unixTime = System.currentTimeMillis() / 1000L;
        return "" + unixTime;
    }
}